﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Profile;
using System.Data;
using Gift.Model.ENUM;

namespace WebSite.admin.product
{
    public partial class category_add : BaseAdmin
    {
   

        // Methods
        protected void Button1_Click(object sender, EventArgs e)
        {
            upload upload = new upload();
            string str = upload.Up(this.file1, "../../uploadfile/product/");
            base.Response.Write(upload.Resup[Convert.ToInt32(str)]);
            this.classpic.Text = upload.s;
        }

        private void Display(string parentid, string space)
        {
            string sql = "Select * From DB_Productclass Where parentid =" + parentid + " and languages=" + info.languages("") + " Order By classorder";
            DataTable table = new DataTable();
            table = SQLAccess.GreatDs(sql).Tables[0];
            if (table.Rows.Count > 0)
            {
                foreach (DataRow row in table.Rows)
                {
                    this.typeidc.Items.Add(new ListItem(space + "" + row["classname"].ToString() + "", row["classid"].ToString().Trim()));
                    this.Display(row["classid"].ToString(), "|-" + space);
                }
            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            string str;
            if (this.typeidc.SelectedItem.Value.ToString() != "0")
            {
                str = this.typeidc.SelectedItem.Value.ToString();
            }
            else
            {
                str = "0";
            }
            if (str.ToString() != "0")
            {
                if (base.Request.QueryString["cid"] != null)
                {
                    string sql = "select top 1 * from DB_Productclass where classid=" + base.Request.QueryString["cid"];
                    DataTable table = new DataTable();
                    table = SQLAccess.GreatDs(sql).Tables[0];
                    if (table.Rows[0]["parentid"].ToString() != str.ToString())
                    {
                        SQLAccess.DoSql("update DB_Productclass set amount=amount+1 where classid=" + str.ToString() + "");
                        SQLAccess.DoSql("update DB_Productclass set amount=amount-1 where classid=" + table.Rows[0]["parentid"].ToString() + "");
                    }
                }
                else
                {
                    SQLAccess.DoSql("update DB_Productclass set amount=amount+1 where classid=" + str.ToString() + "");
                }
            }
            if (base.Request.QueryString["cid"] != null)
            {
                SQLAccess.DoSql(string.Concat(new object[] { 
                "update DB_Productclass set parentid=", str.ToString(), ",classpic='", re.replace(this.classpic.Text), "',classname='", re.replace(this.classname.Value), "',classorder=", re.replace(this.classorder.Value), ",visible='", this.CheckBox1.Checked ? "1" : "0", "',classcontent='", this.WE_NewsContent.Text.Replace("'", "''"), "',classpcs='", re.replace(this.classpcs.Value), "',ishead='", this.CheckBox2.Checked ? "1" : "0", 
                "',classkey='", re.replace(this.classkey.Value), "' where classid=", base.Request.QueryString["cid"], ""
             }));
                base.Response.Write("<script language='javascript'>alert('产品类别修改成功');window.location.href='category.aspx';</script>");
                base.Response.End();
            }
            else
            {
                SQLAccess.DoSql(string.Concat(new object[] { 
                "insert into DB_Productclass (parentid,classname,classorder,visible,classcontent,classpcs,ishead,classkey,classpic,languages) values (", str.ToString(), ",'", re.replace(this.classname.Value), "',", re.replace(this.classorder.Value), ",", this.CheckBox1.Checked ? "1" : "0" , ",'", this.WE_NewsContent.Text.Replace("'", "''"), "','", re.replace(this.classpcs.Value), "',",  this.CheckBox2.Checked ? "1" : "0" , ",'", re.replace(this.classkey.Value), 
                "','", re.replace(this.classpic.Text), "',", info.languages(""), ")"
             }));
                base.Response.Write("<script language='javascript'>alert('产品类别添加成功');window.location.href='category.aspx';</script>");
                base.Response.End();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
         
            this.ImageButton1.Attributes["onclick"] = "return CheckFrm()";
            if (!base.IsPostBack)
            {
                base.ValidateLoginRule(ERule.AddProductClass);
                this.Display("0", "|--");
                if (base.Request.QueryString["cid"] != null)
                {
                    this.upbind();
                }
            }
        }

        private void upbind()
        {
            string sql = "select top 1 * from DB_Productclass where classid=" + base.Request.QueryString["cid"];
            DataTable table = new DataTable();
            table = SQLAccess.GreatDs(sql).Tables[0];
            this.typeidc.SelectedValue = table.Rows[0]["parentid"].ToString();
            this.classname.Value = table.Rows[0]["classname"].ToString();
            this.classorder.Value = table.Rows[0]["classorder"].ToString();
            this.classpcs.Value = table.Rows[0]["classpcs"].ToString();
            this.classkey.Value = table.Rows[0]["classkey"].ToString();
            this.WE_NewsContent.Text = table.Rows[0]["classcontent"].ToString();
            this.CheckBox1.Checked = (bool)table.Rows[0]["visible"];
            this.CheckBox2.Checked = (bool)table.Rows[0]["ishead"];
            this.classpic.Text = table.Rows[0]["classpic"].ToString();
        }


    }
}